//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FormItems.h"
#include "FormMain.h"
#include "FormItemNames.h"
#include "FormItemCategories.h"
#include "FormItemBasicUnits.h"
#include "FormItemDrugGenericNames.h"
#include "FormItemDrugMedForms.h"
#include "FormItemDrugPharmGroups.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DBCtrlsEh"
#pragma link "DBGridEh"
#pragma link "DBGridEhGrouping"
#pragma link "DBLookupEh"
#pragma link "FIBDatabase"
#pragma link "FIBDataSet"
#pragma link "GridsEh"
#pragma link "pFIBDatabase"
#pragma link "pFIBDataSet"
#pragma link "PropFilerEh"
#pragma link "PropStorageEh"
#pragma link "FIBQuery"
#pragma link "pFIBQuery"
#pragma resource "*.dfm"
TItemsForm *ItemsForm;
//---------------------------------------------------------------------------
__fastcall TItemsForm::TItemsForm(TComponent* Owner)
	: TForm(Owner)
{
	PropStorageEh->Section = this->Name;
	Constraints->MinWidth = Width;
	Constraints->MinHeight = Height;
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::AddNewMark(TWinControl* Control)
{
	if(NewMarks.find(Control) == NewMarks.end()) {
		TNewMarkFrame* NewMark = new TNewMarkFrame(NULL);
		NewMark->Parent = Control->Parent;
		NewMark->Left = Control->Left - 4;
		NewMark->Top = Control->Top - 4;
		NewMark->Show();
		NewMarks[Control] = NewMark;
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::DeleteNewMark(TWinControl* Control)
{
	if(NewMarks.find(Control) != NewMarks.end()) {
		TNewMarkFrame* NewMark = NewMarks[Control];
		delete NewMark;
		NewMarks.erase(Control);
	}
}
//---------------------------------------------------------------------------
// FORM SECTION
//---------------------------------------------------------------------------

void __fastcall TItemsForm::FormShow(TObject *Sender)
{
	ResetFiltersButton->Click();
	DataSet->Open();
	ItemNamesDataSet->Open();
	CategoriesDataSet->Open();
	BasicUnitsDataSet->Open();
	GenericNamesDataSet->Open();
	MedFormsDataSet->Open();
	TempRegimesDataSet->Open();
	PharmGroupsSetDataSet->Open();

	MRUCategoriesQuery->ExecQuery();
	while(!MRUCategoriesQuery->Eof) {
		CategoryMruTable[MRUCategoriesQuery->FieldByName("NAME_ID")->AsInteger][MRUCategoriesQuery->FieldByName("CATEGORY_ID")->AsInteger] = MRUCategoriesQuery->FieldByName("CNT")->AsInteger;
		MRUCategoriesQuery->Next();
    }
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::FormHide(TObject *Sender)
{
	if(ReadTransaction->Active) ReadTransaction->Rollback();
	if(WriteTransaction->Active) WriteTransaction->Rollback();
// closing all DataSets on the Form
	for(int i=0; i<ComponentCount; i++) {
		TComponent* Component = Components[i];
		TMetaClass* meta = Component->ClassType();
		while(meta && meta->ClassName() != "TDataSet" && meta->ClassName() != "TFIBTransaction") meta = meta->ClassParent();
		if(meta && meta->ClassName() == "TDataSet") ((TDataSet*)Component)->Close();
		if(meta && meta->ClassName() == "TFIBTransaction") if(((TFIBTransaction*)Component)->Active) ((TFIBTransaction*)Component)->Rollback();
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
	if(EditorPanel->Visible) {
		if(EditorDataChanged()) {
			int result = MessageDlg("Do you want to SAVE the changes you made to \"" + ItemNameDBLookupComboboxEh->Text + "\"?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo<<mbCancel, NULL, mbYes);
			switch(result) {
				case mrYes:
					if(!EditModeOff(true)) CanClose = false;
					break;
				case mrNo:
					EditModeOff(false);
					break;
				default:
					CanClose = false;
					return;
			}
		}
		else EditModeOff(false);
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if(Key == VK_ESCAPE && EditorPanel->Visible && (ActiveControl->ClassName() != "TDBLookupComboboxEh" || !((TDBLookupComboboxEh*)ActiveControl)->ListVisible))
		CloseEditorSpeedButton->Click();
	else if(Key == VK_RETURN && Shift.Contains(ssCtrl) && EditorPanel->Visible)
		SaveButton->Click();
}
//---------------------------------------------------------------------------
// BUTTONS SECTION
//---------------------------------------------------------------------------

void __fastcall TItemsForm::AddToolButtonClick(TObject *Sender)
{
	if(!EditorPanel->Visible) EditModeOn(true);
	else EditModeOff(false);
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::DeleteToolButtonClick(TObject *Sender)
{
	if(MessageDlg("Item: \r\"" + DataSet->FieldByName("NAME")->AsString + "\"\rwill be DELETED. Are you sure?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbNo) == mrYes) {
		DeleteItemQuery->ParamByName("ID")->AsInteger = DataSet->FBN("ID")->AsInteger;
		DeleteItemDrugQuery->ParamByName("ITEM_ID")->AsInteger = DataSet->FBN("ID")->AsInteger;
		DeleteItemQuery->ExecQuery();
		DeleteItemDrugQuery->ExecQuery();
		WriteTransaction->Commit();
		int &count = CategoryMruTable[DataSet->FBN("NAME_ID")->AsInteger][DataSet->FBN("CATEGORY_ID")->AsInteger];
		if(count > 0) count--;
		else throw Exception("error counting CategoryMruTable");
		DataSet->Next();
		if(DataSet->Eof) DataSet->Prior();
		DataSet->FullRefresh();
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::EditToolButtonClick(TObject *Sender)
{
	if(!EditorPanel->Visible) EditModeOn(false);
	else EditModeOff(false);
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::RefreshToolButtonClick(TObject *Sender)
{
	DataSet->FullRefresh();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::SaveButtonClick(TObject *Sender)
{
	if(!EditorDataChanged())
		EditModeOff(false);
	else if(EditorSavedData.ItemID == -1)
		EditModeOff(true);
	else if(MessageDlg("Do you want to SAVE the changes you made to \"" + ItemNameDBLookupComboboxEh->Text + "\"?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbYes) == mrYes)
		EditModeOff(true);
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::CancelButtonClick(TObject *Sender)
{
	if(!EditorDataChanged())
		EditModeOff(false);
	else if(EditorSavedData.ItemID == -1) {
		if(MessageDlg("Do you want to CANCEL the adding of \"" + ItemNameDBLookupComboboxEh->Text + "\"?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbYes) == mrYes)
			EditModeOff(false);
	}
	else if(MessageDlg("Do you want to CANCEL the changes you made to \"" + ItemNameDBLookupComboboxEh->Text + "\"?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbYes) == mrYes)
		EditModeOff(false);
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::CloseEditorSpeedButtonClick(TObject *Sender)
{
	if(EditorDataChanged()) {
		int result = MessageDlg("Do you want to SAVE the changes you made to \"" + ItemNameDBLookupComboboxEh->Text + "\"?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo<<mbCancel, NULL, mbYes);
		if(result == mrYes) EditModeOff(true);
		else if(result == mrNo) EditModeOff(false);
	}
	else EditModeOff(false);
}
//---------------------------------------------------------------------------
// MAIN GRID SECTION
//---------------------------------------------------------------------------

void __fastcall TItemsForm::DBGridEhSortMarkingChanged(TObject *Sender)
{
	String OrderClause;
	for(int i=0; i<DBGridEh->SortMarkedColumns->Count; i++) {
		if(!OrderClause.IsEmpty()) OrderClause = OrderClause + ",";
		OrderClause = OrderClause + DBGridEh->SortMarkedColumns->Items[i]->FieldName;
		if(DBGridEh->SortMarkedColumns->Items[i]->Title->SortMarker == smUpEh) OrderClause = OrderClause + " desc";
	}
	DataSet->Close();
	DataSet->OrderClause = OrderClause;
	DataSet->Open();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::DBGridEhKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if(Key == VK_INSERT) {
		if(AddToolButton->Enabled && !AddToolButton->Down) AddToolButton->Click();
	}
	else if(Key == VK_DELETE && Shift.Contains(ssCtrl)){
		if(DeleteToolButton->Enabled) DeleteToolButton->Click();
	}
	else if(Key == VK_RETURN) {
		if(EditToolButton->Enabled) {
			if(!EditorDataChanged()) EditModeOff(false);
			if(!EditToolButton->Down) EditToolButton->Click();
		}
	}
	else if(Key == VK_F5) {
		if(RefreshToolButton->Enabled) RefreshToolButton->Click();
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::DBGridEhDblClick(TObject *Sender)
{
	if(EditToolButton->Enabled) {
		if(!EditorDataChanged()) EditModeOff(false);
		if(!EditToolButton->Down) EditToolButton->Click();
	}
}
//---------------------------------------------------------------------------
// EDITOR SECTION
//---------------------------------------------------------------------------

void __fastcall TItemsForm::EditModeOn(bool NewItem)
{
	ItemNamesDataSet->Open();
	CategoriesDataSet->Open();
	BasicUnitsDataSet->Open();
	GenericNamesDataSet->Open();
	MedFormsDataSet->Open();
	TempRegimesDataSet->Open();
	PharmGroupsSetDataSet->Open();

	ItemNameLabel->Font->Color = TColor(clWindowText);
	CategoryLabel->Font->Color = TColor(clWindowText);
	BasicUnitLabel->Font->Color = TColor(clWindowText);
	GenericNameLabel->Font->Color = TColor(clWindowText);
	MedicinalFormLabel->Font->Color = TColor(clWindowText);
	DosageLabel->Font->Color = TColor(clWindowText);
	TempRegimeLabel->Font->Color = TColor(clWindowText);
	AuthLevelLabel->Font->Color = TColor(clWindowText);
	PharmGroupsLabel->Font->Color = TColor(clWindowText);

	if(NewItem) {
		EditorCaptionLabel2->Caption = "  Adding new item";
		EditorCaptionLabel3->Caption = "";
		AddToolButton->Down = true;
		DeleteToolButton->Enabled = false;
		EditToolButton->Enabled = false;

		EditorSavedData.ItemID = -1;
		EditorSavedData.ItemName = "";
		EditorSavedData.Category = "";
		EditorSavedData.BasicUnit = "";
		EditorSavedData.IsHazardous = 0;
		EditorSavedData.Description = "";
		EditorSavedData.GenericName = "";
		EditorSavedData.MedForm = "";
		EditorSavedData.Dosage = "";
		EditorSavedData.TempRegime = "";
		EditorSavedData.AuthLevel = "";
		EditorSavedData.IsTraced = 0;
	}
	else {
		EditorCaptionLabel2->Caption = "  Editing item: ";
		EditorCaptionLabel3->Caption = DataSet->FBN("NAME")->AsString;
		AddToolButton->Enabled = false;
		DeleteToolButton->Enabled = false;
		EditToolButton->Down = true;

		EditorSavedData.ItemID = DataSet->FBN("ID")->AsInteger;
		EditorSavedData.ItemName = DataSet->FBN("ITEM_NAME")->AsString;
		EditorSavedData.Category = DataSet->FBN("CATEGORY_NAME")->AsString;
		EditorSavedData.BasicUnit = DataSet->FBN("BASIC_UNIT_NAME")->AsString;
		EditorSavedData.IsHazardous = DataSet->FBN("IS_HAZARDOUS")->AsBoolean;
		EditorSavedData.Description = DataSet->FBN("DESCRIPTION")->AsString;
		EditorSavedData.GenericName = DataSet->FBN("GENERIC_NAME")->AsString;
		EditorSavedData.MedForm = DataSet->FBN("MED_FORM_NAME")->AsString;
		EditorSavedData.Dosage = DataSet->FBN("DOSAGE")->AsString;
		EditorSavedData.TempRegime = DataSet->FBN("TEMP_REGIME_NAME")->AsString;
		EditorSavedData.AuthLevel = DataSet->FBN("AUTHORIZATION_LEVEL")->AsString;
		EditorSavedData.IsTraced = DataSet->FBN("IS_TRACED")->AsBoolean;
	}
	EditorPanel->Show();

	ItemNameDBLookupComboboxEh->Text = EditorSavedData.ItemName;
	CategoryDBLookupComboboxEh->Text = EditorSavedData.Category;
	BasicUnitDBLookupComboboxEh->Text = EditorSavedData.BasicUnit;
	IsHazardousDBCheckBoxEh->Checked = EditorSavedData.IsHazardous;
	DescriptionDBEditEh->Text = EditorSavedData.Description;
	GenericNameDBLookupComboboxEh->Text = EditorSavedData.GenericName;
	MedFormDBLookupComboboxEh->Text = EditorSavedData.MedForm;
	DosageDBEditEh->Text = EditorSavedData.Dosage;
	if(EditorSavedData.TempRegime.IsEmpty()) TempRegimeDBLookupComboboxEh->KeyValue = Null();
	else TempRegimeDBLookupComboboxEh->Text = EditorSavedData.TempRegime;
	if(EditorSavedData.AuthLevel.IsEmpty()) AuthLevelDBComboBoxEh->Clear();
	else AuthLevelDBComboBoxEh->Text = EditorSavedData.AuthLevel;
	TracerDBCheckBoxEh->Checked = EditorSavedData.IsTraced;

	IsPharmGroupsSetChanged = false;

	GenNameAsItemNameDBCheckBoxEh->Checked = (!ItemNameDBLookupComboboxEh->Text.IsEmpty() && ItemNameDBLookupComboboxEh->Text == GenericNameDBLookupComboboxEh->Text);

	SelectNext(EditorPanel, true, true);
}
//---------------------------------------------------------------------------

bool __fastcall TItemsForm::EditModeOff(bool Accept)
{
	try {
		if(Accept && EditorPanel->Visible) {
			SaveButton->SetFocus();
			if(CheckRequiredFields()) {
				if(ItemNameDBLookupComboboxEh->KeyValue.IsNull()) {
					ItemNamesDataSet->Append();
					ItemNamesDataSet->FBN("NAME")->AsString = ItemNameDBLookupComboboxEh->Text.Trim();
					ItemNamesDataSet->Post();
					ItemNameDBLookupComboboxEh->Text = ItemNameDBLookupComboboxEh->Text;
				}
				if(CategoryDBLookupComboboxEh->KeyValue.IsNull()) {
					CategoriesDataSet->Append();
					CategoriesDataSet->FBN("NAME")->AsString = CategoryDBLookupComboboxEh->Text.Trim();
					CategoriesDataSet->Post();
					CategoryDBLookupComboboxEh->Text = CategoryDBLookupComboboxEh->Text;
				}
				if(BasicUnitDBLookupComboboxEh->KeyValue.IsNull()) {
					BasicUnitsDataSet->Append();
					BasicUnitsDataSet->FBN("NAME")->AsString = BasicUnitDBLookupComboboxEh->Text.Trim();
					BasicUnitsDataSet->Post();
					BasicUnitDBLookupComboboxEh->Text = BasicUnitDBLookupComboboxEh->Text;
				}
				if(CategoryDBLookupComboboxEh->KeyValue == 0) {
					if(GenericNameDBLookupComboboxEh->KeyValue.IsNull()) {
						GenericNamesDataSet->Append();
						GenericNamesDataSet->FBN("NAME")->AsString = GenericNameDBLookupComboboxEh->Text.Trim();
						GenericNamesDataSet->Post();
						GenericNameDBLookupComboboxEh->Text = GenericNameDBLookupComboboxEh->Text;
					}
					if(MedFormDBLookupComboboxEh->KeyValue.IsNull()) {
						MedFormsDataSet->Append();
						MedFormsDataSet->FBN("NAME")->AsString = MedFormDBLookupComboboxEh->Text.Trim();
						MedFormsDataSet->Post();
						MedFormDBLookupComboboxEh->Text = MedFormDBLookupComboboxEh->Text;
					}
					if(IsPharmGroupsSetChanged) {
						ClearPharmGroupSetQuery->ParamByName("GENERIC_NAME_ID")->AsInteger = GenericNameDBLookupComboboxEh->KeyValue;
						ClearPharmGroupSetQuery->ExecQuery();
						AddPharmGroupQuery->ParamByName("GENERIC_NAME_ID")->AsInteger = GenericNameDBLookupComboboxEh->KeyValue;
						PharmGroupsSetDataSet->First();
						while(!PharmGroupsSetDataSet->Eof) {
							if(PharmGroupsSetDataSet->FBN("IS_SELECTED")->AsInteger) {
								AddPharmGroupQuery->ParamByName("PHARM_GROUP_ID")->AsInteger = PharmGroupsSetDataSet->FBN("ID")->AsInteger;
								AddPharmGroupQuery->ExecQuery();
							}
							PharmGroupsSetDataSet->Next();
						}
					}
				}

				if(EditorSavedData.ItemID >= 0) UpdateInsertItemQuery->ParamByName("ID")->AsInteger = EditorSavedData.ItemID;
				UpdateInsertItemQuery->ParamByName("NAME_ID")->AsInteger = ItemNameDBLookupComboboxEh->KeyValue;
				UpdateInsertItemQuery->ParamByName("CATEGORY_ID")->AsInteger = CategoryDBLookupComboboxEh->KeyValue;
				UpdateInsertItemQuery->ParamByName("DESCRIPTION")->AsString = DescriptionDBEditEh->Text.Trim();
				UpdateInsertItemQuery->ParamByName("BASIC_UNIT_ID")->AsInteger = BasicUnitDBLookupComboboxEh->KeyValue;
				UpdateInsertItemQuery->ParamByName("IS_HAZARDOUS")->AsInteger = IsHazardousDBCheckBoxEh->Checked;
				UpdateInsertItemQuery->ExecQuery();
				if(EditorSavedData.ItemID < 0) EditorSavedData.ItemID = UpdateInsertItemQuery->FieldByName("ID")->AsInteger;

				if(CategoryDBLookupComboboxEh->KeyValue == 0) {
					UpdateInsertItemDrugQuery->ParamByName("ITEM_ID")->AsInteger = EditorSavedData.ItemID;
					UpdateInsertItemDrugQuery->ParamByName("GENERIC_NAME_ID")->AsInteger = GenericNameDBLookupComboboxEh->KeyValue;
					UpdateInsertItemDrugQuery->ParamByName("MED_FORM_ID")->AsInteger = MedFormDBLookupComboboxEh->KeyValue;
					UpdateInsertItemDrugQuery->ParamByName("DOSAGE")->AsString = DosageDBEditEh->Text;
					UpdateInsertItemDrugQuery->ParamByName("TEMP_REGIME_ID")->AsInteger = TempRegimeDBLookupComboboxEh->KeyValue;
					UpdateInsertItemDrugQuery->ParamByName("AUTHORIZATION_LEVEL")->AsString = AuthLevelDBComboBoxEh->Text;
					UpdateInsertItemDrugQuery->ParamByName("IS_TRACED")->AsInteger = TracerDBCheckBoxEh->Checked;
					UpdateInsertItemDrugQuery->ExecQuery();
				}
				else {
					DeleteItemDrugQuery->ParamByName("ITEM_ID")->AsInteger = EditorSavedData.ItemID;
					DeleteItemDrugQuery->ExecQuery();
				}
				WriteTransaction->Commit();
				DataSet->FullRefresh();
				DataSet->Locate("ID", EditorSavedData.ItemID, TLocateOptions());
			}
			else return false;
			CategoryMruTable[ItemNameDBLookupComboboxEh->KeyValue][CategoryDBLookupComboboxEh->KeyValue]++;
		}
		else {
			if(WriteTransaction->Active) WriteTransaction->Rollback();
		}
	}
	catch(Exception& E) {
		if(mrNo == MessageDlg("Can't save changes.\rError: " + E.Message + "\r\rClose anyway?", mtError, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbNo))
			return false;
	}
	AddToolButton->Enabled = true;
	DeleteToolButton->Enabled = true;
	EditToolButton->Enabled = true;
	AddToolButton->Down = false;
	EditToolButton->Down = false;

	EditorPanel->Hide();
	DBGridEh->SetFocus();
	return true;
}
//---------------------------------------------------------------------------

bool __fastcall TItemsForm::EditorDataChanged()
{
	if (ItemNameDBLookupComboboxEh->Text != EditorSavedData.ItemName ||
		CategoryDBLookupComboboxEh->Text != EditorSavedData.Category ||
		BasicUnitDBLookupComboboxEh->Text != EditorSavedData.BasicUnit ||
		IsHazardousDBCheckBoxEh->Checked != EditorSavedData.IsHazardous ||
		DescriptionDBEditEh->Text != EditorSavedData.Description ||
		GenericNameDBLookupComboboxEh->Text != EditorSavedData.GenericName ||
		MedFormDBLookupComboboxEh->Text != EditorSavedData.MedForm ||
		DosageDBEditEh->Text != EditorSavedData.Dosage ||
		TempRegimeDBLookupComboboxEh->Text != EditorSavedData.TempRegime ||
		AuthLevelDBComboBoxEh->Text != EditorSavedData.AuthLevel ||
		TracerDBCheckBoxEh->Checked != EditorSavedData.IsTraced  ||
		IsPharmGroupsSetChanged) return true;
	return false;
}
//---------------------------------------------------------------------------

bool __fastcall TItemsForm::CheckRequiredFields()
{
	TWinControl* EmptyControl = NULL;
	if(ItemNameDBLookupComboboxEh->Text.Trim().IsEmpty()) {
		ItemNameLabel->Font->Color = TColor(clRed);
		if(!EmptyControl) EmptyControl = ItemNameDBLookupComboboxEh;
		ItemNameDBLookupComboboxEh->SetFocus();
	}
	if(CategoryDBLookupComboboxEh->Text.Trim().IsEmpty()) {
		CategoryLabel->Font->Color = TColor(clRed);
		if(!EmptyControl) EmptyControl = CategoryDBLookupComboboxEh;
	}
	if(BasicUnitDBLookupComboboxEh->Text.Trim().IsEmpty()) {
		BasicUnitLabel->Font->Color = TColor(clRed);
		if(!EmptyControl) EmptyControl = BasicUnitDBLookupComboboxEh;
	}
	if(CategoryDBLookupComboboxEh->KeyValue == 0) {
		if(GenericNameDBLookupComboboxEh->Text.Trim().IsEmpty()) {
			GenericNameLabel->Font->Color = TColor(clRed);
			if(!EmptyControl) EmptyControl = GenericNameDBLookupComboboxEh;
		}
		if(MedFormDBLookupComboboxEh->Text.Trim().IsEmpty()) {
			MedicinalFormLabel->Font->Color = TColor(clRed);
			if(!EmptyControl) EmptyControl = MedFormDBLookupComboboxEh;
		}
		if(DosageDBEditEh->Text.Trim().IsEmpty()) {
			DosageLabel->Font->Color = TColor(clRed);
			if(!EmptyControl) EmptyControl = DosageDBEditEh;
		}
		if(TempRegimeDBLookupComboboxEh->Text.Trim().IsEmpty()) {
			TempRegimeLabel->Font->Color = TColor(clRed);
			if(!EmptyControl) EmptyControl = TempRegimeDBLookupComboboxEh;
		}
		if(AuthLevelDBComboBoxEh->Text.Trim().IsEmpty()) {
			AuthLevelLabel->Font->Color = TColor(clRed);
			if(!EmptyControl) EmptyControl = AuthLevelDBComboBoxEh;
		}
		if(PharmGroupsSetDBGridEh->SumList->SumCollection->Items[0]->SumValue == 0) {
			PharmGroupsLabel->Font->Color = TColor(clRed);
			if(!EmptyControl) EmptyControl = PharmGroupsSetDBGridEh;
		}
	}

	if(!EmptyControl) {
		ItemNameLabel->Font->Color = TColor(clWindowText);
		CategoryLabel->Font->Color = TColor(clWindowText);
		BasicUnitLabel->Font->Color = TColor(clWindowText);
		GenericNameLabel->Font->Color = TColor(clWindowText);
		MedicinalFormLabel->Font->Color = TColor(clWindowText);
		DosageLabel->Font->Color = TColor(clWindowText);
		TempRegimeLabel->Font->Color = TColor(clWindowText);
		AuthLevelLabel->Font->Color = TColor(clWindowText);
		PharmGroupsLabel->Font->Color = TColor(clWindowText);
	}
	else {
		MessageDlg("Please enter all required data", mtError, TMsgDlgButtons()<<mbOK, NULL);
		EmptyControl->SetFocus();
	}

	return !EmptyControl;
}
//---------------------------------------------------------------------------
// EDITOR CONTROLS SECTION
//---------------------------------------------------------------------------

void __fastcall TItemsForm::DBLookupComboboxEhKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	TDBLookupComboboxEh* DBLookupComboboxEh = (TDBLookupComboboxEh*)ActiveControl;
	if(Key == VK_ESCAPE) DBLookupComboboxEh->SelectAll();
	else if(Key == VK_RETURN) {
		Key = NULL;
		if(DBLookupComboboxEh->ListVisible) DBLookupComboboxEh->CloseUp(true);
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::EhKeyPress(TObject *Sender, System::WideChar &Key)
{
	if(Key == '\r') {
		Key = NULL;
		SelectNext(ActiveControl, true, true);
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::DBLookupComboboxEhDropDownButtonClick(TObject *Sender, bool &Handled)
{
	TDBLookupComboboxEh* DBLookupComboboxEh = (TDBLookupComboboxEh*)ActiveControl;
	if(!DBLookupComboboxEh->ListVisible) DBLookupComboboxEh->DropDown();
	else DBLookupComboboxEh->CloseUp(false);
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::DBComboBoxEhDropDownButtonClick(TObject *Sender, bool &Handled)
{
	TDBComboBoxEh* DBComboBoxEh = (TDBComboBoxEh*)ActiveControl;
	if(!DBComboBoxEh->ListVisible) DBComboBoxEh->DropDown();
	else DBComboBoxEh->CloseUp(false);
}
//---------------------------------------------------------------------------
// ITEM_NAME

void __fastcall TItemsForm::ItemNameDBLookupComboboxEhValueChanged(TObject *Sender)
{
	bool EmptyItemName = ItemNameDBLookupComboboxEh->Text.IsEmpty();
	bool NewItemName = !EmptyItemName && ItemNameDBLookupComboboxEh->KeyValue.IsNull();
	if(EmptyItemName) {
		CategoryLabel->Enabled = false;
		CategoryDBLookupComboboxEh->Enabled = false;
		CategoryDBLookupComboboxEh->Text = "";
		DeleteNewMark(ItemNameDBLookupComboboxEh);
	}
	else if(NewItemName) {
		ItemNameLabel->Font->Color = clWindowText;
		CategoryLabel->Enabled = true;
		CategoryDBLookupComboboxEh->Enabled = true;
		CategoryDBLookupComboboxEh->TabStop = true;
		CategoryDBLookupComboboxEh->Text = "";
		AddNewMark(ItemNameDBLookupComboboxEh);
	}
	else {
		ItemNameLabel->Font->Color = clWindowText;
		CategoryLabel->Enabled = true;
		CategoryDBLookupComboboxEh->Enabled = true;

		int max_cnt = -1;
		int category_id = -1;
		for(CATEGORY_CNT::iterator it = CategoryMruTable[ItemNameDBLookupComboboxEh->KeyValue].begin();
			it != CategoryMruTable[ItemNameDBLookupComboboxEh->KeyValue].end();
			++it)
		{
			if(it->second > max_cnt) {
				max_cnt = it->second;
				category_id = it->first;
			}
		}
		if(category_id >= 0) {
			CategoryDBLookupComboboxEh->KeyValue = category_id;
			CategoryDBLookupComboboxEh->TabStop = false;
		}
		else {
			if(ItemNameDBLookupComboboxEh->Focused()) CategoryDBLookupComboboxEh->Text = "";
			CategoryDBLookupComboboxEh->TabStop = true;
		}

		DeleteNewMark(ItemNameDBLookupComboboxEh);
	}
}
//---------------------------------------------------------------------------
// CATEGORY

void __fastcall TItemsForm::CategoriesDBLookupComboboxEhValueChanged(TObject *Sender)
{
	bool EmptyCategory = CategoryDBLookupComboboxEh->Text.IsEmpty();
	bool NewCategory = !EmptyCategory && CategoryDBLookupComboboxEh->KeyValue.IsNull();
	bool IsMedicineCategory = CategoryDBLookupComboboxEh->KeyValue == 0;

	GenNameAsItemNameDBCheckBoxEh->Checked = false;
	if(EmptyCategory) {
		GenNameAsItemNameDBCheckBoxEh->Enabled = false;

		BasicUnitLabel->Enabled = false;
		BasicUnitDBLookupComboboxEh->Enabled = false;
		IsHazardousDBCheckBoxEh->Enabled = false;
		DescriptionLabel->Enabled = false;
		DescriptionDBEditEh->Enabled = false;

		MedDetailsGroupBox->Enabled = false;
		MedDetailsGroupBox->Font->Color = clGray;
		GenericNameLabel->Enabled = false;
		GenericNameDBLookupComboboxEh->Enabled = false;
		MedicinalFormLabel->Enabled = false;
		MedFormDBLookupComboboxEh->Enabled = false;
		DosageLabel->Enabled = false;
		DosageDBEditEh->Enabled = false;
		TempRegimeLabel->Enabled = false;
		TempRegimeDBLookupComboboxEh->Enabled = false;
		AuthLevelLabel->Enabled = false;
		AuthLevelDBComboBoxEh->Enabled = false;
		TracerDBCheckBoxEh->Enabled = false;
		PharmGroupsSpeedButton->Enabled = false;
		PharmGroupsSetDBGridEh->Enabled = false;

		DeleteNewMark(CategoryDBLookupComboboxEh);
	}
	else if(NewCategory) {
		CategoryLabel->Font->Color = clWindowText;
		GenNameAsItemNameDBCheckBoxEh->Enabled = false;

		BasicUnitLabel->Enabled = true;
		BasicUnitDBLookupComboboxEh->Enabled = true;
		IsHazardousDBCheckBoxEh->Enabled = true;
		DescriptionLabel->Enabled = true;
		DescriptionDBEditEh->Enabled = true;

		MedDetailsGroupBox->Enabled = false;
		MedDetailsGroupBox->Font->Color = clGray;
		GenericNameLabel->Enabled = false;
		GenericNameDBLookupComboboxEh->Enabled = false;
		MedicinalFormLabel->Enabled = false;
		MedFormDBLookupComboboxEh->Enabled = false;
		DosageLabel->Enabled = false;
		DosageDBEditEh->Enabled = false;
		TempRegimeLabel->Enabled = false;
		TempRegimeDBLookupComboboxEh->Enabled = false;
		AuthLevelLabel->Enabled = false;
		AuthLevelDBComboBoxEh->Enabled = false;
		TracerDBCheckBoxEh->Enabled = false;
		PharmGroupsSpeedButton->Enabled = false;
		PharmGroupsSetDBGridEh->Enabled = false;

		AddNewMark(CategoryDBLookupComboboxEh);
	}
	else if(IsMedicineCategory) {
		CategoryLabel->Font->Color = clWindowText;
		GenNameAsItemNameDBCheckBoxEh->Enabled = true;

		BasicUnitLabel->Enabled = true;
		BasicUnitDBLookupComboboxEh->Enabled = true;
		IsHazardousDBCheckBoxEh->Enabled = true;
		DescriptionLabel->Enabled = true;
		DescriptionDBEditEh->Enabled = true;

		MedDetailsGroupBox->Enabled = true;
		MedDetailsGroupBox->Font->Color = clWindowText;
		GenericNameLabel->Enabled = true;
		GenericNameDBLookupComboboxEh->Enabled = true;
		MedicinalFormLabel->Enabled = true;
		MedFormDBLookupComboboxEh->Enabled = true;
		DosageLabel->Enabled = true;
		DosageDBEditEh->Enabled = true;
		TempRegimeLabel->Enabled = true;
		TempRegimeDBLookupComboboxEh->Enabled = true;
		AuthLevelLabel->Enabled = true;
		AuthLevelDBComboBoxEh->Enabled = true;
		TracerDBCheckBoxEh->Enabled = true;
		PharmGroupsSpeedButton->Enabled = true;
		if(GenericNameDBLookupComboboxEh->Text.IsEmpty()) PharmGroupsSetDBGridEh->Enabled = false;
		else PharmGroupsSetDBGridEh->Enabled = true;

		DeleteNewMark(CategoryDBLookupComboboxEh);
	}
	else {
		CategoryLabel->Font->Color = clWindowText;
		GenNameAsItemNameDBCheckBoxEh->Enabled = false;

		BasicUnitLabel->Enabled = true;
		BasicUnitDBLookupComboboxEh->Enabled = true;
		IsHazardousDBCheckBoxEh->Enabled = true;
		DescriptionLabel->Enabled = true;
		DescriptionDBEditEh->Enabled = true;

		MedDetailsGroupBox->Enabled = false;
		MedDetailsGroupBox->Font->Color = clGray;
		GenericNameLabel->Enabled = false;
		GenericNameDBLookupComboboxEh->Enabled = false;
		MedicinalFormLabel->Enabled = false;
		MedFormDBLookupComboboxEh->Enabled = false;
		DosageLabel->Enabled = false;
		DosageDBEditEh->Enabled = false;
		TempRegimeLabel->Enabled = false;
		TempRegimeDBLookupComboboxEh->Enabled = false;
		AuthLevelLabel->Enabled = false;
		AuthLevelDBComboBoxEh->Enabled = false;
		TracerDBCheckBoxEh->Enabled = false;
		PharmGroupsSpeedButton->Enabled = false;
		PharmGroupsSetDBGridEh->Enabled = false;

		DeleteNewMark(CategoryDBLookupComboboxEh);
	}
}
//---------------------------------------------------------------------------
// GENERIC_NAME AS ITEM_NAME

void __fastcall TItemsForm::GenNameAsItemNameDBCheckBoxEhClick(TObject *Sender)
{
	if(GenNameAsItemNameDBCheckBoxEh->Checked) {
		GenericNameLabel->Enabled = false;
		GenericNameDBLookupComboboxEh->Enabled = false;
		GenericNameDBLookupComboboxEh->Text = ItemNameDBLookupComboboxEh->Text;
	}
	else {
		GenericNameLabel->Enabled = true;
		GenericNameDBLookupComboboxEh->Enabled = true;
		GenericNameDBLookupComboboxEh->Text = "";
	}
}
//---------------------------------------------------------------------------
// BASIC_UNIT

void __fastcall TItemsForm::BasicUnitDBLookupComboboxEhValueChanged(TObject *Sender)
{
	bool EmptyBasicUnit = BasicUnitDBLookupComboboxEh->Text.IsEmpty();
	bool NewBasicUnit = !EmptyBasicUnit && BasicUnitDBLookupComboboxEh->KeyValue.IsNull();
	if(!EmptyBasicUnit) BasicUnitLabel->Font->Color = clWindowText;
	if(NewBasicUnit)
		AddNewMark(BasicUnitDBLookupComboboxEh);
	else
		DeleteNewMark(BasicUnitDBLookupComboboxEh);
}
//---------------------------------------------------------------------------
// GENERIC_NAME

void __fastcall TItemsForm::GenericNameDBLookupComboboxEhValueChanged(TObject *Sender)
{
	if(GenericNameDBLookupComboboxEh->Focused()) {
		PharmGroupsSetDataSet->ParamByName("GENERIC_NAME_ID")->Value = GenericNameDBLookupComboboxEh->KeyValue;
		PharmGroupsSetDataSet->FullRefresh();
		IsPharmGroupsSetChanged = false;
	}

	bool EmptyGenericName = GenericNameDBLookupComboboxEh->Text.IsEmpty();
	bool NewGenericName = !EmptyGenericName && GenericNameDBLookupComboboxEh->KeyValue.IsNull();

	if(EmptyGenericName) {
		PharmGroupsLabel->Enabled = false;
		PharmGroupsSpeedButton->Enabled = false;
		PharmGroupsSetDBGridEh->Enabled = false;
		DeleteNewMark(GenericNameDBLookupComboboxEh);
	}
	else if(NewGenericName) {
		GenericNameLabel->Font->Color = clWindowText;
		PharmGroupsLabel->Enabled = true;
		PharmGroupsSpeedButton->Enabled = true;
		PharmGroupsSetDBGridEh->Enabled = true;
		PharmGroupsSetDBGridEh->TabStop = true;
		AddNewMark(GenericNameDBLookupComboboxEh);
	}
	else {
		GenericNameLabel->Font->Color = clWindowText;
		PharmGroupsLabel->Enabled = true;
		PharmGroupsSpeedButton->Enabled = true;
		PharmGroupsSetDBGridEh->Enabled = true;
		PharmGroupsSetDBGridEh->TabStop = false;
		DeleteNewMark(GenericNameDBLookupComboboxEh);
	}
}
//---------------------------------------------------------------------------
// MED_FORM

void __fastcall TItemsForm::MedFormDBLookupComboboxEhValueChanged(TObject *Sender)
{
	bool EmptyMedForm = MedFormDBLookupComboboxEh->Text.IsEmpty();
	bool NewMedForm = !EmptyMedForm && MedFormDBLookupComboboxEh->KeyValue.IsNull();
	if(!EmptyMedForm) MedicinalFormLabel->Font->Color = clWindowText;
	if(NewMedForm)
		AddNewMark(MedFormDBLookupComboboxEh);
	else
		DeleteNewMark(MedFormDBLookupComboboxEh);
}
//---------------------------------------------------------------------------
// TEMP_REGIME

void __fastcall TItemsForm::TempRegimeDBLookupComboboxEhChange(TObject *Sender)
{
	bool EmptyTempRegime = TempRegimeDBLookupComboboxEh->Text.IsEmpty();
	if(!EmptyTempRegime) TempRegimeLabel->Font->Color = clWindowText;
}
//---------------------------------------------------------------------------
// PHARMGROUPS GRID SECTION
//---------------------------------------------------------------------------

void __fastcall TItemsForm::PharmGroupsSetDBGridEhEnter(TObject *Sender)
{
	PharmGroupsSetDataSet->Filtered = false;
	PharmGroupsSetDBGridEh->Columns->Items[1]->Visible = true;
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::PharmGroupsSetDBGridEhExit(TObject *Sender)
{
	PharmGroupsSetDataSet->Filtered = true;
	int Fields[2] = {0, 1};
	bool Orders[2] = {false, true};
	PharmGroupsSetDataSet->DoSortEx(Fields, 1, Orders, 1);
	PharmGroupsSetDBGridEh->Columns->Items[1]->Visible = false;
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::PharmGroupsSetDBGridEhDblClick(TObject *Sender)
{
	IsPharmGroupsSetChanged = true;
	PharmGroupsSetDataSet->Edit();
	PharmGroupsSetDataSet->FBN("IS_SELECTED")->AsInteger = !PharmGroupsSetDataSet->FBN("IS_SELECTED")->AsInteger;
	PharmGroupsSetDataSet->Post();
	PharmGroupsLabel->Caption = "        Pharmaceutical groups (" + PharmGroupsSetDBGridEh->SumList->SumCollection->Items[0]->SumValue + " selected)";
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::PharmGroupsSetDBGridEhCellClick(TColumnEh *Column)
{
	if(Column->FieldName == "IS_SELECTED" || GetKeyState(VK_CONTROL)&0x8000) PharmGroupsSetDBGridEhDblClick(PharmGroupsSetDBGridEh);
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::PharmGroupsSetDBGridEhKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if(Key == ' ') PharmGroupsSetDBGridEhDblClick(PharmGroupsSetDBGridEh);
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::PharmGroupsSetDBGridEhSumListAfterRecalcAll(TObject *Sender)
{
	PharmGroupsLabel->Caption = "        Pharmaceutical groups (" + PharmGroupsSetDBGridEh->SumList->SumCollection->Items[0]->SumValue + " selected)";
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::PharmGroupsSetDBGridEhColumnsGetCellParams(TObject *Sender, bool EditMode, TColCellParamsEh *Params)
{
	if(PharmGroupsSetDataSet->FBN("IS_SELECTED")->AsInteger == 1) {
		Params->Background = TColor(0xFFF0E0);
		Params->Font->Style = TFontStyles()<<fsBold;
		Params->Font->Color = clWindowText;
	}
	else {
		Params->Background = PharmGroupsSetDBGridEh->Color;//(0x00E0FFFF);
		Params->Font = PharmGroupsSetDBGridEh->Font;//Params->Font->Style = TFontStyles();
	}
}
//---------------------------------------------------------------------------


void __fastcall TItemsForm::CategoriesDBLookupComboboxEhExit(TObject *Sender)
{
//	String NewText = CategoryDBLookupComboboxEh->Text.Trim();
//	bool IsNewItemName = !ItemNameDBLookupComboboxEh->Enabled;
//	bool IsNewCategoryName = !NewText.IsEmpty() && CategoryDBLookupComboboxEh->KeyValue.IsNull();
//	bool IsItemCategoryChanged = CategoryDBLookupComboboxEh->KeyValue != ItemNamesDataSet->FBN("CATEGORY_ID")->Value;
//	if((IsNewItemName && IsNewCategoryName &&
//	mrYes == MessageDlg("New Category: \"" + NewText + "\"\rwill be added.\r\rAre you sure?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbYes))
//	|| (!IsNewItemName && IsNewCategoryName &&
//	mrYes == MessageDlg("New Category: \"" + NewText + "\"\rwill be added and selected for Item: \"" + ItemNameDBLookupComboboxEh->Text + "\"\r\rAre you sure?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbYes))
//	|| (!IsNewItemName && !IsNewCategoryName && IsItemCategoryChanged &&
//	mrYes == MessageDlg("Category for Item: \"" + ItemNameDBLookupComboboxEh->Text + "\"\rwill be changed to: \"" + NewText + "\"\r\rAre you sure?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbYes)))
//	{
//		if(IsNewCategoryName) {
//			CategoriesDataSet->Append();
//			CategoriesDataSet->FieldByName("NAME")->AsString = NewText;
//			CategoriesDataSet->Post();
//			CategoriesDataSet->FullRefresh();
//			CategoryDBLookupComboboxEh->Text = NewText;
//			AddNewMark(CategoriesDBLookupComboboxEh);
//		}
//		if(IsNewItemName) {
//			ItemNamesDataSet->Append();
//			ItemNamesDataSet->FBN("NAME")->Value = ItemNameDBLookupComboboxEh->Text;
//			ItemNamesDataSet->FBN("CATEGORY_ID")->Value = CategoryDBLookupComboboxEh->KeyValue;
//			ItemNamesDataSet->Post();
//			ItemNamesDataSet->FullRefresh();
//			ItemNameDBLookupComboboxEh->Text = ItemNameDBLookupComboboxEh->Text;
//			ItemNameDBLookupComboboxEh->Enabled = true;
//		}
//		else {
//			ItemNamesDataSet->Edit();
//			ItemNamesDataSet->FBN("CATEGORY_ID")->Value = CategoryDBLookupComboboxEh->KeyValue;
//			ItemNamesDataSet->Post();
//		}
//		if(CategoryDBLookupComboboxEh->KeyValue == 0)
//			GenNameAsItemNameDBCheckBoxEh->SetFocus();
//		else
//			BasicUnitDBLookupComboboxEh->SetFocus();
//	}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

void __fastcall TItemsForm::ItemNameDBLookupComboboxEhEditButtonClick(TObject *Sender, bool &Handled)
{
	ItemNamesForm->ShowModal();
	ItemNamesDataSet->FullRefresh();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::CategoriesDBLookupComboboxEhEditButtonClick(TObject *Sender, bool &Handled)
{
	ItemCategoriesForm->ShowModal();
	CategoriesDataSet->FullRefresh();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::BasicUnitDBLookupComboboxEhEditButtonClick(TObject *Sender, bool &Handled)
{
	ItemBasicUnitsForm->ShowModal();
	BasicUnitsDataSet->FullRefresh();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::GenericNameDBLookupComboboxEhEditButtonClick(TObject *Sender, bool &Handled)
{
	ItemDrugGenericNamesForm->ShowModal();
	GenericNamesDataSet->FullRefresh();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::MedFormDBLookupComboboxEhEditButtonClick(TObject *Sender, bool &Handled)
{
	ItemDrugMedFormsForm->ShowModal();
	MedFormsDataSet->FullRefresh();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::PharmGroupsSpeedButtonClick(TObject *Sender)
{
	ItemDrugPharmGroupsForm->ShowModal();
	PharmGroupsSetDataSet->FullRefresh();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::FiltersToolButtonClick(TObject *Sender)
{
	if(FiltersPanel->Visible) {
		FiltersToolButton->Down = false;
		ItemNameFilterDataSet->Close();
		CategoriesFilterDataSet->Close();
		GenericNamesFilterDataSet->Close();
		PharmGroupsFilterDataSet->Close();
		TempRegimesFilterDataSet->Close();
		FiltersPanel->Hide();
		ResetFiltersButton->Click();
	}
	else {
		FiltersToolButton->Down = true;
		ItemNameFilterDataSet->Open();
		CategoriesFilterDataSet->Open();
		GenericNamesFilterDataSet->Open();
		PharmGroupsFilterDataSet->Open();
		TempRegimesFilterDataSet->Open();

		ItemNameFilterDBLookupComboboxEh->Text = "";
		CategoryFilterDBLookupComboboxEh->KeyValue = -1;
		HazardousOnlyDBCheckBoxEh->Checked = false;
		GenericNameFilterDBLookupComboboxEh->KeyValue = -1;
		PharmGroupFilterDBLookupComboboxEh->KeyValue = -1;
		TempRegimeFilterDBLookupComboboxEh->KeyValue = -1;
		AuthLevelFilterDBComboBoxEh->ItemIndex = 0;
		TracerOnlyDBCheckBoxEh->Checked = false;
		FiltersPanel->Show();
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::CloseFiltersSpeedButtonClick(TObject *Sender)
{
	FiltersToolButton->Click();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::ApplyFiltersButtonClick(TObject *Sender)
{
	DataSet->ParamByName("FLT_NAME")->AsString = ItemNameFilterDBLookupComboboxEh->Text;
	DataSet->ParamByName("FLT_CATEGORY_ID")->AsInteger = CategoryFilterDBLookupComboboxEh->KeyValue;
	DataSet->ParamByName("FLT_HAZARDOUS")->AsBoolean = HazardousOnlyDBCheckBoxEh->Checked;
	DataSet->ParamByName("FLT_GENERIC_NAME_ID")->AsInteger = GenericNameFilterDBLookupComboboxEh->KeyValue;
	DataSet->ParamByName("FLT_PHARM_GROUP_ID")->AsInteger = PharmGroupFilterDBLookupComboboxEh->KeyValue;
	DataSet->ParamByName("FLT_TEMP_REGIME_ID")->AsInteger = TempRegimeFilterDBLookupComboboxEh->KeyValue;
	DataSet->ParamByName("FLT_AUTH_LEVEL")->AsString = AuthLevelFilterDBComboBoxEh->KeyItems->Strings[AuthLevelFilterDBComboBoxEh->ItemIndex];
	DataSet->ParamByName("FLT_TRACED")->AsBoolean = TracerOnlyDBCheckBoxEh->Checked;
	DataSet->FullRefresh();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::ResetFiltersButtonClick(TObject *Sender)
{
	ItemNameFilterDBLookupComboboxEh->Text = "";
	CategoryFilterDBLookupComboboxEh->KeyValue = -1;
	HazardousOnlyDBCheckBoxEh->Checked = false;
	GenericNameFilterDBLookupComboboxEh->KeyValue = -1;
	PharmGroupFilterDBLookupComboboxEh->KeyValue = -1;
	TempRegimeFilterDBLookupComboboxEh->KeyValue = -1;
	AuthLevelFilterDBComboBoxEh->ItemIndex = 0;
	TracerOnlyDBCheckBoxEh->Checked = false;
	ApplyFiltersButton->Click();
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::CategoryFilterDBLookupComboboxEhKeyValueChanged(TObject *Sender)
{
	if(/*CategoryFilterDBLookupComboboxEh->KeyValue.IsNull() || */CategoryFilterDBLookupComboboxEh->KeyValue == -1 || CategoryFilterDBLookupComboboxEh->KeyValue == 0) {
		MedicineFilterGroupBoxLabel->Enabled = true;
		GenericNameFilterLabel->Enabled = true;
		GenericNameFilterDBLookupComboboxEh->Enabled = true;
		PharmGroupFilterLabel->Enabled = true;
		PharmGroupFilterDBLookupComboboxEh->Enabled = true;
		TempRegimeFilterLabel->Enabled = true;
		TempRegimeFilterDBLookupComboboxEh->Enabled = true;
		AuthLevelFilterLabel->Enabled = true;
		AuthLevelFilterDBComboBoxEh->Enabled = true;
		TracerOnlyDBCheckBoxEh->Enabled = true;
	}
	else {
		MedicineFilterGroupBoxLabel->Enabled = false;
		GenericNameFilterLabel->Enabled = false;
		GenericNameFilterDBLookupComboboxEh->Enabled = false;
		PharmGroupFilterLabel->Enabled = false;
		PharmGroupFilterDBLookupComboboxEh->Enabled = false;
		TempRegimeFilterLabel->Enabled = false;
		TempRegimeFilterDBLookupComboboxEh->Enabled = false;
		AuthLevelFilterLabel->Enabled = false;
		AuthLevelFilterDBComboBoxEh->Enabled = false;
		TracerOnlyDBCheckBoxEh->Enabled = false;
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::FiltersPanelResize(TObject *Sender)
{
	LeftFiltersPanel->Width = FiltersPanel->Width / 2;
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::DBGridEhColumns0AdvDrawDataCell(TCustomDBGridEh *Sender,
		  TGridCoord &Cell, TGridCoord &AreaCell, TColumnEh *Column,
		  const TRect &ARect, TColCellParamsEh *&Params, bool &Processed)

{
	if(EditorPanel->Visible && DataSet->FBN("ID")->AsInteger == EditorSavedData.ItemID && Column->FieldName == "NAME") {
		int marg = (ARect.Height() - EditMarkImage->Height) / 2;
		int x = ARect.Right - EditMarkImage->Width - marg;
		int y = ARect.Top + marg;
		DBGridEh->DefaultDrawColumnDataCell(Cell, AreaCell, Column, ARect, Params);
		DBGridEh->Canvas->Draw(x, y, EditMarkImage->Picture->Graphic);
		Processed = true;
	}
}
//---------------------------------------------------------------------------

void __fastcall TItemsForm::TempRegimeDBLookupComboboxEhEnter(TObject *Sender)
{
	if(TempRegimeDBLookupComboboxEh->Text.IsEmpty()) TempRegimeDBLookupComboboxEh->KeyValue = 0;
}
//---------------------------------------------------------------------------

